<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Manager_model extends CI_Model {

	public function getmysqlversion(){
		
	}
	//添加一个用户
	public function addone($post){
		$data = array(
			'passid' => $post['passid'],
			'password' => md5($post['password']),
			'name' => $post['name'],
			'level' => $post['level'],
			'email' => $post['email']
		);
		return $this->db->insert('manager',$data);
	}

	//获取所有用户
	public function getall(){
		return $this->db->get('manager');
	}

	//根据id获取用户
	public function getsta($id){
		$res = $this->db->get_where('manager',array('id'=>$id));
		return $res->row();
	}

	//根据账号取回用户
	public function getuserbyname($name){
		$query = $this->db->get_where('manager',array('passid'=>$name));
		return $query->row();
	}

	//停用启用某用户
	public function setstatus($id){
		$status = $this->getsta($id)->status;
		$this->db->where('id', $id);
		if($status){
			$data =array('status'=>0);
			$bool = $this->db->update('manager', $data);
		}else{
			$data =array('status'=>1);
			$bool = $this->db->update('manager', $data);
		}
		return $bool;
	}

	//更新用户登录时间
	public function replacelogtime($id){
		$time = date("Y-m-d h:i:s",time());
		$this->db->where('id', $id);
		$this->db->update('manager', array('lastlogintime'=>$time));
	}

	//设置用户头像
	public function setuserpic($str,$id){
		$this->db->where('id', $id);
		$res = $this->db->update('manager',array('userpic'=>$str));
		return $res;
	}

	//返回用户头像
	public function getuserpic($id){
		$res = $this->db->get_where('manager',array('id'=>$id));
		return $res->row();
	}

	//更新用户资料
	public function setuserinfo($post){
		$post['name']!=''?$data['name']=$post['name']:'';
		$post['email']!=''?$data['email']=$post['email']:'';
		$post['college']!=''?$data['college']=$post['college']:'';
		$post['location']!=''?$data['location']=$post['location']:'';
		$post['skills']!=''?$data['skills']=$post['skills']:'';
		$post['notes']!=''?$data['notes']=$post['notes']:'';

		$this->db->where('id',$post['id']);
		return $this->db->update('manager', $data);
	}

	//更新网站设置
	public function setsysinfo($post){
		$query = $this->db->get('sysinfo');
		if($query->row()){
			$id = $query->row()->id;
		}else{
			$id = 1;
		}
		$data['id'] = $id;
		$post['weburl']!=''?$data['web']=$post['weburl']:'';
		$post['title']!=''?$data['title']=$post['title']:'';
		$post['keywords']!=''?$data['keywords']=$post['keywords']:'';
		$post['description']!=''?$data['description']=$post['description']:'';
		$post['copyright']!=''?$data['copyright']=$post['copyright']:'';
		$post['icpid']!=''?$data['icp']=$post['icpid']:'';

		return $this->db->replace('sysinfo', $data);
	}

	//获取网站设置
	public function getsysinfo(){
		$query = $this->db->get('sysinfo');
		return $query->row();
	}

	//网站功能开关处理
	public function setonoff($i){
		$info = $this->db->get('sysinfo')->row_array();
		$this->db->where('id',$info['id']);

		if($info[$i]){
			return $this->db->update('sysinfo', array($i=>'0'));
		}else{
			return $this->db->update('sysinfo', array($i=>'1'));
		}
	}

}


